package com.ultizen.farm.preloader
{
	import com.ultizen.farm.component.loading.LoaderItem;
	import com.ultizen.farm.core.ApplicationConfig;
	import com.ultizen.farm.core.PreloaderView;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import com.ultizen.farm.utils.text.stringf;
	[Embed(source="Loading.swf",symbol="PreloaderUI")]
	public dynamic class PreloaderUI extends Sprite implements PreloaderView
	{
		private var bar:Sprite;
		private var txt:TextField;
		public function PreloaderUI()
		{
			txt = this.percentTxt as TextField;
			bar = this.loadingBar as Sprite;
			bar.scaleX = 0;
			var random:int = Math.round(Math.random() * 4);
			var url:String = stringf(ApplicationConfig.instance.assets["preload"], random);
			var loader:LoaderItem = new LoaderItem(url);
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.start();  
			addChildAt(loader.content, 0);
			addChildAt(this.logo, 0);
		}

		public function updatePercent(percent:Number):void
		{
			bar.scaleX = percent;
			txt.text = Math.floor(percent * 100) +"%";
		}
		
		public function onComplete():void
		{
			bar.scaleX = 1;
			txt.text = "100%"
		}
		private function onLoadComplete(event:Event):void
		{
			
		}
		
	}
}